package com.service;

import com.data_object.TidToCountDO;
import com.data_object.TrackDO;
import com.repository.RelationRepo;
import com.repository.TrackRepo;
import com.util.Log;
import com.view_object.ListTrackView;
import com.view_object.PaginationView;
import com.view_object.StudioTrackListView;
import com.view_object.UserVO;

import javax.management.relation.Relation;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**组合得到的数据，拼接成最终希望展示的结果
 * @author yuisama
 * @date 2023/03/08 15:15
 **/
public class TrackService {
    private final TrackRepo trackRepo = new TrackRepo();
    private final RelationRepo relationRepo = new RelationRepo();

    public StudioTrackListView listOfUser(UserVO userVO, int page) {
        StudioTrackListView resultView = new StudioTrackListView();
        resultView.currentUser = userVO;
        //0.规定每页共有5条
        int countPerPage = 5;

        //3.查询出页面信息
        int count = trackRepo.selectCountByUid(userVO.uid);
        int totalPage = count / countPerPage;
        if(count % countPerPage >0){
            //向上取整
            totalPage++;
        }
        if(page <1){
            Log.println("页面信息小于最小值，调整为1");
            page =1;
        }
        if(page >totalPage){
            Log.println("页面信息大于最大值，调整为最大值");
            page = totalPage;
        }
        //这里一直出现500错误，必须先调整page，再更改offset
        int offset = (page - 1)* countPerPage;


        //传入一个用户
        //1.从数据库表中查询即可
        List<TrackDO> list = trackRepo.selectListByUidLimitOffset(userVO.uid,countPerPage,offset);
        Log.println("从数据库表中查询到的结果："+ list);
        if(!list.isEmpty()) {
            Log.println("结果不为空，所以需要继续查询关联数量");
            //如果不为空
            //2.根据结果，查询每个track对应的refCount引用次数
            //2.1先从list中得到每一个tid
            List<Integer> tidList = new ArrayList<>();
            for (TrackDO trackDO : list) {
                tidList.add(trackDO.tid);
            }
            //2.2拿着tidList从relations表中得到需要的结果Map<tid,refCount>
            Map<Integer, Integer> tidToRefCountMap = new HashMap<>();
            List<TidToCountDO> tidToCountDOList = relationRepo.selectCountByTidListGroupByTidList(tidList);
            Log.println("查询relations表得到的结果："+tidToCountDOList);
            for (TidToCountDO tidToCountDO : tidToCountDOList) {
                tidToRefCountMap.put(tidToCountDO.tid, tidToCountDO.refCount);
            }
            Log.println("tid to ref count map:"+ tidToRefCountMap);

            //3.将TrackDO ->ListTrackView
            List<ListTrackView> resultList = new ArrayList<>();
            for (TrackDO trackDO : list) {
                ListTrackView view = new ListTrackView(trackDO, tidToRefCountMap.getOrDefault(trackDO.tid, 0));
                resultList.add(view);
            }
            Log.println("转换成准备输出的结果：" + resultList);
            resultView.trackList = resultList;
        }else{
            //空的就不用处理了
            Log.println("结果为空，所以关联数量就不需要查询了");
            resultView.trackList = new ArrayList<>();
        }


        PaginationView paginationView = new PaginationView();
        paginationView.countPerPage = countPerPage;
        paginationView.currentPage = page;
        paginationView.totalPage = totalPage;
        resultView.pagination = paginationView;

        return resultView;
    }
}

